package com.fmi.cloud.biz;

import android.content.Context;

import com.fmi.cloud.R;
import com.fmi.cloud.util.OrangeDataHandler;
import com.fmi.cloud.util.OrangeErrorHandler;
import com.fmi.cloud.util.StringUtil;
import com.fmi.cloud.util.VolleyHttpUtil;
import com.fmi.cloud.util.YcToastHelper;
import com.fmi.cloud.view.LoadDialog;
import com.fmi.thirdsdk.jpush.JPushUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ZeroVoid on 2018-3-9.
 */

public class KxHttpBiz {

    //快销
    public static void sendSms(final Context context, String phone, VolleyHttpUtil.ResponseCallBack callback) {
        LoadDialog.show(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(context, R.string.path_sms), map, callback);
    }


    //房米快销
    public static void login(final Context ctx, String phone, String smsCode) {
        if (StringUtil.isBlank(phone)) {
            YcToastHelper.getInstance()._toast("请先输入手机号");
            return;
        } else if (StringUtil.isBlank(smsCode)) {
            YcToastHelper.getInstance()._toast("请先输入验证码");
            return;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("smscode", smsCode);
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(ctx, R.string.path_login), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
                if (isOk) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
                    if (map != null) {
                        String agencyId = (String) map.get("agencyId");
                        String brokerId = (String) map.get("brokerId");
                        CommonSp.setAgentID(agencyId);
                        CommonSp.setBrokerID(brokerId);
                        try {
                            String ret = response.getString("entity");
                            CommonSp.setUserInfo(ret);
                            KxHttpBiz.getAgentInfo(ctx);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    public static void getAgentInfo(final Context context) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("agencyId", CommonSp.getAgentID());
        LoadDialog.show(context);
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(context, R.string.path_agent), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(context);
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
                if (isOk) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
                    if (map != null) {
                        String provinceId = (String) map.get("provinceId");
                        String cityId = (String) map.get("cityId");
                        CommonSp.setProvinceID(provinceId);
                        CommonSp.setCityID(cityId);
                        handleTags(context, provinceId, cityId);
                        FmHttpBiz.jumpMain(context);
//                        if (isOk2) {
//                            YcToastHelper.getInstance()._toast("登录成功");
//                        } else {
//                            YcToastHelper.getInstance()._toast("设置标签失败");
//                        }
                    } else {
                        YcToastHelper.getInstance()._toast("数据为空");
                    }
                }
            }
        });
    }

    private static void handleTags(Context context, String provinceID, String cityID) {
        Set<String> set = new HashSet<>();
        String agentID = CommonSp.getAgentID();
        String brokerID = CommonSp.getBrokerID();
        agentID = JPushUtil.isValidTagAndAlias(agentID);
        if (agentID != null) {
            set.add(agentID);
        }
        brokerID = JPushUtil.isValidTagAndAlias(brokerID);
        if (brokerID != null) {
            set.add(brokerID);
        }
        provinceID = JPushUtil.isValidTagAndAlias(provinceID);
        if (provinceID != null) {
            set.add(provinceID);
        }
        cityID = JPushUtil.isValidTagAndAlias(cityID);
        if (cityID != null) {
            set.add(cityID);
        }
        JPushInterface.setTags(context, 0, set);
    }


}
